package com.zzh.custom.loading;

import android.app.Activity;
import android.content.Context;
import android.graphics.Path;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import androidx.fragment.app.Fragment;

import com.zzh.custom.R;

import java.util.ArrayList;
import java.util.List;

public class LoadingOperation implements Loading {
    private String TAG = LoadingOperation.class.getSimpleName();

    private Context mContext;
    private ViewGroup mRootView;

    private View mCoverView;
    private View mView;

    private Status mStatus = Status.SUCCESS;

    public LoadingOperation(Activity activity) {
        this.mContext = activity;
        this.mRootView = (ViewGroup) activity.getWindow().getDecorView();
        if (mRootView.getChildCount() > 0) {
            mView = mRootView.getChildAt(0);
        } else {
            mView = mRootView;
        }
    }

    public LoadingOperation(Fragment fragment) {
        this.mContext = fragment.getContext();
        mView = fragment.getView();
        this.mRootView = (ViewGroup) mView.getParent();
    }

    public LoadingOperation(View view) {
        this.mContext = view.getContext();
        mView = view;
        this.mRootView = (ViewGroup) view.getParent();
    }

    private LoadingViewBuild mLoadingViewBuild = new LoadingViewBuild() {
        @Override
        public View getLoadingView(Loading loading) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.loading_loading, null);
            return view;
        }

        @Override
        public View getErrorView(Loading loading) {
            return LayoutInflater.from(mContext).inflate(R.layout.loading_error, null);
        }

        @Override
        public View getEmptyView(Loading loading) {
            return LayoutInflater.from(mContext).inflate(R.layout.loading_empty, null);
        }
    };


    @Override
    public void showLoading() {
        if (mStatus == Status.LOADING) return;
        clearView();
        mStatus = Status.LOADING;
        updateStatus();
    }

    private View createView(Status status) {
        if (mLoadingViewBuild == null) {
            throw new NullPointerException("没有创建布局构建器 " + LoadingViewBuild.class.getName());
        }
        View view;
        switch (status) {
            case LOADING:
                view = mLoadingViewBuild.getLoadingView(this);
                break;
            case EMPTY:
                view = mLoadingViewBuild.getEmptyView(this);
                break;
            case ERROR:
                view = mLoadingViewBuild.getErrorView(this);
                break;
            case ONGOING:
                view = new OngoingView(mContext);
                break;
            case SUCCESS:
            default:
                view = null;
        }
        if (view != null) {
            configViewLayoutParams(view);
        }
        return view;
    }

    private void configViewLayoutParams(View view) {
        ViewGroup.LayoutParams layoutParams = mView.getLayoutParams();
        int width = mView.getWidth();
        int height = mView.getHeight();
        if (width > 0) {
            layoutParams.width = width;
        }
        if (height > 0) {
            layoutParams.height = height;
        }
        view.setLayoutParams(layoutParams);
    }

    private void clearView() {
        if (mCoverView == null) return;
        if (mRootView.indexOfChild(mCoverView) != -1) {
            mRootView.removeView(mCoverView);
        }
        mCoverView = null;
    }

    @Override
    public void showSuccess() {
        if (mStatus == Status.SUCCESS) return;
        clearView();
        mStatus = Status.SUCCESS;
    }

    @Override
    public void showError() {
        if (mStatus == Status.ERROR) return;
        mStatus = Status.ERROR;
        updateStatus();
    }

    private void updateStatus() {
        mRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mRootView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                clearView();
                mCoverView = createView(mStatus);
                mRootView.addView(mCoverView, mRootView.getChildCount());
                if (mCoverView instanceof OngoingView) {
                    OngoingView ongoingView = (OngoingView) mCoverView;
                    ongoingView.configOperationView(mView);
                }
            }
        });
        mRootView.requestLayout();
    }

    @Override
    public void showEmpty() {
        if (mStatus == Status.EMPTY) return;
        mStatus = Status.EMPTY;
        updateStatus();
    }

    @Override
    public void showOngoing() {
        if (mStatus == Status.ONGOING) return;
        mStatus = Status.ONGOING;
        updateStatus();
    }

    @Override
    public void setLoadingViewBuild(LoadingViewBuild loadingViewBuild) {
        this.mLoadingViewBuild = loadingViewBuild;
    }

    @Override
    public Context getContext() {
        return mContext;
    }

    @Override
    public View getRootView() {
        return mRootView;
    }

    @Override
    public Status getStatus() {
        return mStatus;
    }

}
